<!DOCTYPE HTML>
<html lang="zh-CN" class="sidebar-visible no-js light">
    <head>
        <!-- Book generated using mdBook -->
        <meta charset="UTF-8">
        <title> Section 4.4 - PurpleDragonBookAnswer</title>


        <!-- Custom HTML head -->
        
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="Purple Dragon Book Answer Online Edition">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="theme-color" content="#ffffff" />

        <link rel="icon" href="../../favicon.svg">
        <link rel="shortcut icon" href="../../favicon.png">
        <link rel="stylesheet" href="../../css/variables.css">
        <link rel="stylesheet" href="../../css/general.css">
        <link rel="stylesheet" href="../../css/chrome.css">
        <link rel="stylesheet" href="../../css/print.css" media="print">

        <!-- Fonts -->
        <link rel="stylesheet" href="../../FontAwesome/css/font-awesome.css">
        <link rel="stylesheet" href="../../fonts/fonts.css">

        <!-- Highlight.js Stylesheets -->
        <link rel="stylesheet" href="../../highlight.css">
        <link rel="stylesheet" href="../../tomorrow-night.css">
        <link rel="stylesheet" href="../../ayu-highlight.css">

        <!-- Custom theme stylesheets -->

    </head>
    <body>
        <!-- Provide site root to javascript -->
        <script type="text/javascript">
            var path_to_root = "../../";
            var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
        </script>

        <!-- Work around some values being stored in localStorage wrapped in quotes -->
        <script type="text/javascript">
            try {
                var theme = localStorage.getItem('mdbook-theme');
                var sidebar = localStorage.getItem('mdbook-sidebar');

                if (theme.startsWith('"') && theme.endsWith('"')) {
                    localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
                }

                if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
                    localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
                }
            } catch (e) { }
        </script>

        <!-- Set the theme before any content is loaded, prevents flash -->
        <script type="text/javascript">
            var theme;
            try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
            if (theme === null || theme === undefined) { theme = default_theme; }
            var html = document.querySelector('html');
            html.classList.remove('no-js')
            html.classList.remove('light')
            html.classList.add(theme);
            html.classList.add('js');
        </script>

        <!-- Hide / unhide sidebar before it is displayed -->
        <script type="text/javascript">
            var html = document.querySelector('html');
            var sidebar = 'hidden';
            if (document.body.clientWidth >= 1080) {
                try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
                sidebar = sidebar || 'visible';
            }
            html.classList.remove('sidebar-visible');
            html.classList.add("sidebar-" + sidebar);
        </script>

        <nav id="sidebar" class="sidebar" aria-label="Table of contents">
            <div class="sidebar-scrollbox">
                <ol class="chapter"><li class="chapter-item expanded "><a href="../../preface.html"><strong aria-hidden="true">1.</strong> Preface</a></li><li class="chapter-item expanded "><a href="../../ch01/ch01.html"><strong aria-hidden="true">2.</strong> Chapter 1</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../../ch01/1.1/1.1.html"><strong aria-hidden="true">2.1.</strong> Section 1.1</a></li><li class="chapter-item expanded "><a href="../../ch01/1.3/1.3.html"><strong aria-hidden="true">2.2.</strong> Section 1.3</a></li><li class="chapter-item expanded "><a href="../../ch01/1.6/1.6.html"><strong aria-hidden="true">2.3.</strong> Section 1.6</a></li></ol></li><li class="chapter-item expanded "><a href="../../ch02/ch02.html"><strong aria-hidden="true">3.</strong> Chapter 2</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../../ch02/key-point/key-point.html"><strong aria-hidden="true">3.1.</strong> KeyPoints</a></li><li class="chapter-item expanded "><a href="../../ch02/2.2/2.2.html"><strong aria-hidden="true">3.2.</strong> Section 2.2</a></li><li class="chapter-item expanded "><a href="../../ch02/2.3/2.3.html"><strong aria-hidden="true">3.3.</strong> Section 2.3</a></li><li class="chapter-item expanded "><a href="../../ch02/2.4/2.4.html"><strong aria-hidden="true">3.4.</strong> Section 2.4</a></li><li class="chapter-item expanded "><a href="../../ch02/2.6/2.6.html"><strong aria-hidden="true">3.5.</strong> Section 2.6</a></li><li class="chapter-item expanded "><a href="../../ch02/2.8/2.8.html"><strong aria-hidden="true">3.6.</strong> Section 2.8</a></li></ol></li><li class="chapter-item expanded "><a href="../../ch03/ch03.html"><strong aria-hidden="true">4.</strong> Chapter 3</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../../ch03/key-point/key-point.html"><strong aria-hidden="true">4.1.</strong> KeyPoints</a></li><li class="chapter-item expanded "><a href="../../ch03/3.1/3.1.html"><strong aria-hidden="true">4.2.</strong> Section 3.1</a></li><li class="chapter-item expanded "><a href="../../ch03/3.3/3.3.html"><strong aria-hidden="true">4.3.</strong> Section 3.3</a></li><li class="chapter-item expanded "><a href="../../ch03/3.4/3.4.html"><strong aria-hidden="true">4.4.</strong> Section 3.4</a></li><li class="chapter-item expanded "><a href="../../ch03/3.5/3.5.html"><strong aria-hidden="true">4.5.</strong> Section 3.5</a></li><li class="chapter-item expanded "><a href="../../ch03/3.6/3.6.html"><strong aria-hidden="true">4.6.</strong> Section 3.6</a></li><li class="chapter-item expanded "><a href="../../ch03/3.7/3.7.html"><strong aria-hidden="true">4.7.</strong> Section 3.7</a></li><li class="chapter-item expanded "><a href="../../ch03/3.8/3.8.html"><strong aria-hidden="true">4.8.</strong> Section 3.8</a></li><li class="chapter-item expanded "><a href="../../ch03/3.9/3.9.html"><strong aria-hidden="true">4.9.</strong> Section 3.9</a></li></ol></li><li class="chapter-item expanded "><a href="../../ch04/ch04.html"><strong aria-hidden="true">5.</strong> Chapter 4</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../../ch04/key-point/key-point.html"><strong aria-hidden="true">5.1.</strong> KeyPoints</a></li><li class="chapter-item expanded "><a href="../../ch04/4.2/4.2.html"><strong aria-hidden="true">5.2.</strong> Section 4.2</a></li><li class="chapter-item expanded "><a href="../../ch04/4.3/4.3.html"><strong aria-hidden="true">5.3.</strong> Section 4.3</a></li><li class="chapter-item expanded "><a href="../../ch04/4.4/4.4.html" class="active"><strong aria-hidden="true">5.4.</strong> Section 4.4</a></li><li class="chapter-item expanded "><a href="../../ch04/4.5/4.5.html"><strong aria-hidden="true">5.5.</strong> Section 4.5</a></li><li class="chapter-item expanded "><a href="../../ch04/4.6/4.6.html"><strong aria-hidden="true">5.6.</strong> Section 4.6</a></li><li class="chapter-item expanded "><a href="../../ch04/4.7/4.7.html"><strong aria-hidden="true">5.7.</strong> Section 4.7</a></li></ol></li><li class="chapter-item expanded "><a href="../../ch05/ch05.html"><strong aria-hidden="true">6.</strong> Chapter 5</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../../ch05/5.1/5.1.html"><strong aria-hidden="true">6.1.</strong> Section 5.1</a></li><li class="chapter-item expanded "><a href="../../ch05/5.2/5.2.html"><strong aria-hidden="true">6.2.</strong> Section 5.2</a></li><li class="chapter-item expanded "><a href="../../ch05/5.3/5.3.html"><strong aria-hidden="true">6.3.</strong> Section 5.3</a></li><li class="chapter-item expanded "><a href="../../ch05/5.4/5.4.html"><strong aria-hidden="true">6.4.</strong> Section 5.4</a></li><li class="chapter-item expanded "><a href="../../ch05/5.5/5.5.html"><strong aria-hidden="true">6.5.</strong> Section 5.5</a></li></ol></li><li class="chapter-item expanded "><a href="../../ch06/ch06.html"><strong aria-hidden="true">7.</strong> Chapter 6</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../../ch06/6.1/6.1.html"><strong aria-hidden="true">7.1.</strong> Section 6.1</a></li><li class="chapter-item expanded "><a href="../../ch06/6.2/6.2.html"><strong aria-hidden="true">7.2.</strong> Section 6.2</a></li><li class="chapter-item expanded "><a href="../../ch06/6.3/6.3.html"><strong aria-hidden="true">7.3.</strong> Section 6.3</a></li><li class="chapter-item expanded "><a href="../../ch06/6.4/6.4.html"><strong aria-hidden="true">7.4.</strong> Section 6.4</a></li><li class="chapter-item expanded "><a href="../../ch06/6.5/6.5.html"><strong aria-hidden="true">7.5.</strong> Section 6.5</a></li><li class="chapter-item expanded "><a href="../../ch06/6.6/6.6.html"><strong aria-hidden="true">7.6.</strong> Section 6.6</a></li><li class="chapter-item expanded "><a href="../../ch06/6.7/6.7.html"><strong aria-hidden="true">7.7.</strong> Section 6.7</a></li></ol></li><li class="chapter-item expanded "><a href="../../ch07/ch07.html"><strong aria-hidden="true">8.</strong> Chapter 7</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../../ch07/7.2/7.2.html"><strong aria-hidden="true">8.1.</strong> Section 7.2</a></li><li class="chapter-item expanded "><a href="../../ch07/7.3/7.3.html"><strong aria-hidden="true">8.2.</strong> Section 7.3</a></li><li class="chapter-item expanded "><a href="../../ch07/7.3/7.4.html"><strong aria-hidden="true">8.3.</strong> Section 7.4</a></li><li class="chapter-item expanded "><a href="../../ch07/7.5/7.5.html"><strong aria-hidden="true">8.4.</strong> Section 7.5</a></li><li class="chapter-item expanded "><a href="../../ch07/7.6/7.6.html"><strong aria-hidden="true">8.5.</strong> Section 7.6</a></li><li class="chapter-item expanded "><a href="../../ch07/7.7/7.7.html"><strong aria-hidden="true">8.6.</strong> Section 7.7</a></li></ol></li><li class="chapter-item expanded "><a href="../../ch08/ch08.html"><strong aria-hidden="true">9.</strong> Chapter 8</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../../ch08/8.2/8.2.html"><strong aria-hidden="true">9.1.</strong> Section 8.2</a></li><li class="chapter-item expanded "><a href="../../ch08/8.3/8.3.html"><strong aria-hidden="true">9.2.</strong> Section 8.3</a></li><li class="chapter-item expanded "><a href="../../ch08/8.4/8.4.html"><strong aria-hidden="true">9.3.</strong> Section 8.4</a></li><li class="chapter-item expanded "><a href="../../ch08/8.5/8.5.html"><strong aria-hidden="true">9.4.</strong> Section 8.5</a></li></ol></li></ol>
            </div>
            <div id="sidebar-resize-handle" class="sidebar-resize-handle"></div>
        </nav>

        <div id="page-wrapper" class="page-wrapper">

            <div class="page">
                                <div id="menu-bar-hover-placeholder"></div>
                <div id="menu-bar" class="menu-bar sticky bordered">
                    <div class="left-buttons">
                        <button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
                            <i class="fa fa-bars"></i>
                        </button>
                        <button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
                            <i class="fa fa-paint-brush"></i>
                        </button>
                        <ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
                            <li role="none"><button role="menuitem" class="theme" id="light">Light (default)</button></li>
                            <li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
                            <li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
                            <li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
                            <li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
                        </ul>
                        <button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
                            <i class="fa fa-search"></i>
                        </button>
                    </div>

                    <h1 class="menu-title">PurpleDragonBookAnswer</h1>

                    <div class="right-buttons">
                        <a href="../../print.html" title="Print this book" aria-label="Print this book">
                            <i id="print-button" class="fa fa-print"></i>
                        </a>

                    </div>
                </div>

                <div id="search-wrapper" class="hidden">
                    <form id="searchbar-outer" class="searchbar-outer">
                        <input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
                    </form>
                    <div id="searchresults-outer" class="searchresults-outer hidden">
                        <div id="searchresults-header" class="searchresults-header"></div>
                        <ul id="searchresults">
                        </ul>
                    </div>
                </div>

                <!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
                <script type="text/javascript">
                    document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
                    document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
                    Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
                        link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
                    });
                </script>

                <div id="content" class="content">
                    <main>
                        <h1 id="44-节的练习"><a class="header" href="#44-节的练习">4.4 节的练习</a></h1>
<h3 id="441"><a class="header" href="#441">4.4.1</a></h3>
<p>为下面的每个文法设计一个预测分析器，并给出预测分析表。你可能先要对文法进行提取左公因子或者消除左递归的操作。</p>
<p>练习 4.2.2 中 1 - 7 中的文法。</p>
<h4 id="解答"><a class="header" href="#解答">解答</a></h4>
<ol>
<li>
<p>S -&gt; 0 S 1 | 0 1</p>
<p>step1. 提取左公因子</p>
<pre><code> S -&gt; 0 A
 A -&gt; S 1 | 1
</code></pre>
<p>step2. 消除左递归</p>
<pre><code> S -&gt; 0 A
 A -&gt; 0 A 1 | 1
</code></pre>
<p>step3. 预测分析表</p>
<table>
     <thead>
         <tr>
             <th rowspan="2">非终结符号</th>
             <th colspan="3">输入符号</th>
         </tr>
         <tr>
             <th>0</th>
             <th>1</th>
             <th>$</th>
         </tr>
     </thead>
     <tbody>
         <tr>
             <th>S</th>
             <td>S -> 0 A</td>
             <td></td>
             <td></td>
         </tr>
         <tr>
             <th>A</th>
             <td>A -> 0 A 1</td>
             <td>A -> 1</td>
             <td></td>
         </tr>
     </tbody>
 </table>
</li>
<li>
<p>S -&gt; + S S | * S S | a</p>
<p>step1. 无左公因子</p>
<p>step2. 无左递归</p>
<p>step3. 预测分析表</p>
<table>
     <thead>
         <tr>
             <th rowspan="2">非终结符号</th>
             <th colspan="4">输入符号</th>
         </tr>
         <tr>
             <th>+</th>
             <th>*</th>
             <th>a</th>
             <th>$</th>
         </tr>
     </thead>
     <tbody>
         <tr>
             <th>S</th>
             <td>S -> + S S</td>
             <td>S -> * S S</td>
             <td>S -> a</td>
             <td></td>
         </tr>
     </tbody>
 </table>
</li>
<li>
<p>! S -&gt; S (S) S | ε</p>
<p>step1. 无左公因子</p>
<p>step2. 消除左递归</p>
<pre><code> S -&gt; A
 A -&gt; (S) S A | ε
</code></pre>
<p>step3. 预测分析表</p>
<table>
     <thead>
         <tr>
             <th rowspan="2">非终结符号</th>
             <th colspan="3">输入符号</th>
         </tr>
         <tr>
             <th>(</th>
             <th>)</th>
             <th>$</th>
         </tr>
     </thead>
     <tbody>
         <tr>
             <th>S</th>
             <td>S -> A</td>
             <td>S -> A</td>
             <td>S -> A</td>
         </tr>
         <tr>
             <th>A</th>
             <td>A -> (S) S A<br/>A -> ε</td>
             <td>A -> ε</td>
             <td>A -> ε</td>
         </tr>
     </tbody>
 </table>
</li>
<li>
<p>! S -&gt; S + S | S S | (S) | S * | a</p>
<p>step1. 提取左公因子</p>
<pre><code> S -&gt; SA | (S) | a
 A -&gt; +S | S | *
</code></pre>
<p>进一步提取终结符</p>
<pre><code> S -&gt; SA | T
 A -&gt; +S | S | *
 T -&gt; (S) | a  
</code></pre>
<p>step2. 消除左递归(根据 p135 的算法 4.19)</p>
<pre><code> i = 1
         S -&gt; TB
         B -&gt; AB | ε
         
 i = 2
     j = 1
         A -&gt; +S | TB | *
         
 i = 3
     j = 1
         无需处理
     j = 2
         无需处理
</code></pre>
<p>得到最终的产生式</p>
<pre><code> S -&gt; TB
 B -&gt; AB | ε
 A -&gt; +S | TB | *
 T -&gt; (S) | a  
</code></pre>
<p>step3. first &amp;&amp; follow</p>
<pre><code> first(T) = [(, a]
 first(A) = [+, *] + first(T) =[+, *, (, a]
 first(B) = [ε] + first(A) = [ε, +, *, (, a]
 first(S) = first(T) = [(, a]
 
 follow(T) = [$, +, *, (, a]
 follow(A) = [$, +, *, (, ), a]
 follow(B) = [$]
 follow(S) = [$, +, *, (, ), a]
</code></pre>
<p>step4. 预测分析表</p>
<table>
     <thead>
         <tr>
             <th rowspan="2">非终结符号</th>
             <th colspan="6">输入符号</th>
         </tr>
         <tr>
             <th>(</th>
             <th>)</th>
             <th>+</th>
             <th>*</th>
             <th>a</th>
             <th>$</th>
         </tr>
     </thead>
     <tbody>
         <tr>
             <th>S</th>
             <td>S -> TB</td>
             <td></td>
             <td></td>
             <td></td>
             <td>S -> TB</td>
             <td></td>
         </tr>
         <tr>
             <th>B</th>
             <td>B -> AB</td>
             <td></td>
             <td>B -> AB</td>
             <td>B -> AB</td>
             <td>B -> AB</td>
             <td>B -> ε</td>
         </tr>
         <tr>
             <th>A</th>
             <td>A -> TB</td>
             <td></td>
             <td>A -> +S</td>
             <td>A -> \*</td>
             <td>A -> TB</td>
             <td></td>
         </tr>
         <tr>
             <th>T</th>
             <td>T -> (S)</td>
             <td></td>
             <td></td>
             <td></td>
             <td>T -> a </td>
             <td></td>
         </tr>
     </tbody>
 </table>
</li>
<li>
<p>S -&gt; (L) | a 以及 L -&gt; L, S | S</p>
<p>step1. 无左公因子</p>
<p>step2. 消除左递归</p>
<pre><code> S -&gt; (L) | a
 L -&gt; SA
 A -&gt; ,SA | ε
</code></pre>
<p>step3. first &amp;&amp; follow</p>
<pre><code>first(S) = [(, a]
first(L) = [(, a]
first(A) = [\,, ε]   # \, 表示文法中的非终结符

follow(S) = [\,, ), $]
follow(L) = [)]
follow(A) = [)]
</code></pre>
<p>step4. 预测分析表</p>
 <table>
     <thead>
         <tr>
             <th rowspan="2">非终结符号</th>
             <th colspan="5">输入符号</th>
         </tr>
         <tr>
             <th>(</th>
             <th>)</th>
             <th>,</th>
             <th>a</th>
             <th>$</th>
         </tr>
     </thead>
     <tbody>
         <tr>
             <th>S</th>
             <td>S -> (L)</td>
             <td></td>
             <td></td>
             <td>S -> a</td>
             <td></td>
         </tr>
 		<tr>
             <th>L</th>
             <td>L -> SA</td>
             <td></td>
             <td></td>
             <td>L -> SA</td>
             <td></td>
         </tr>
         <tr>
             <th>A</th>
             <td></td>
             <td></td>
             <td>A -> ,SA</td>
             <td></td>
             <td></td>
         </tr>
     </tbody>
 </table>
</li>
<li>
<p>grammar for boolean expressions:</p>
<pre><code>bexpr -&gt; bexpr or bterm | bterm
bterm -&gt; bterm and bfactor | bfactor
bfactor -&gt; not bfactor | ( bexpr ) | true | false
</code></pre>
<p>step1. 无左公因子</p>
<p>step2. 消除左递归</p>
<pre><code>bexpr -&gt; bterm bexpr'
bexpr' -&gt; or bterm bexpr' | ε
bterm -&gt; bfactor bterm'
bterm' -&gt; and bfactor bterm' | ε
bfactor -&gt; not bfactor | (bexpr) | true | false
</code></pre>
<p>step3. first &amp;&amp; follow</p>
<pre><code>first(bexpr) = first(bterm) = first(bfactor) = [not, (, true, false]
first(bexpr') = [or, ε]
first(bterm') = [and, ε]

follow(bexpr) = follow(bexpr') = [), $]
follow(bterm) = follow(bterm') = [or, $]
follow(bfactor) = [and, $]
  
</code></pre>
<p>step4. 预测分析表</p>
<table>
     <thead>
         <tr>
             <th rowspan="2">非终结符号</th>
             <th colspan="8">输入符号</th>
         </tr>
         <tr>
             <th>and</th>
             <th>or</th>
             <th>not</th>
             <th>(</th>
             <th>)</th>
             <th>true</th>
             <th>false</th>
             <th>$</th>
         </tr>
     </thead>
     <tbody>
         <tr>
             <th>bexpr</th>
             <td></td>
             <td></td>
             <td>bexpr -> bterm bexpr'</td>
             <td>bexpr -> bterm bexpr'</td>
             <td></td>
             <td>bexpr -> bterm bexpr'</td>
             <td>bexpr -> bterm bexpr'</td>
             <td></td>
         </tr>
         <tr>
             <th>bexpr'</th>
             <td></td>
             <td>bexpr' -> or bterm bexpr'</td>
             <td></td>
             <td></td>
             <td>bexpr' -> ε</td>
             <td></td>
             <td></td>
             <td>bexpr' -> ε</td>
         </tr>
         <tr>
             <th>bterm</th>
             <td></td>
             <td></td>
             <td>bterm -> bfactor bterm'</td>
             <td>bterm -> bfactor bterm'</td>
             <td></td>
             <td>bterm -> bfactor bterm'</td>
             <td>bterm -> bfactor bterm'</td>
             <td></td>
         </tr>
         <tr>
             <th>bterm'</th>
             <td></td>
             <td>bterm' -> and bfactor bterm'</td>
             <td></td>
             <td></td>
             <td>bterm' -> ε</td>
             <td></td>
             <td></td>
             <td>bterm' -> ε</td>
         </tr>
         <tr>
             <th>bfactor</th>
             <td></td>
             <td></td>
             <td>bfactor -> not bfactor</td>
             <td>bfactor -> (bexpr)
             <td></td>
             <td>bfactor -> true</td>
             <td>bfactor -> false</td>
             <td></td>
         </tr>
     </tbody>
 </table>
</li>
</ol>
<h3 id="442-"><a class="header" href="#442-">4.4.2 ！！</a></h3>
<p>有没有可能通过某种方法修改练习 4.2.1 中的文法，构造出一个与该练习中的语言（运算分量为 a 的后缀表达式）对应的预测分析器？</p>
<h4 id="解答-1"><a class="header" href="#解答-1">解答</a></h4>
<pre><code>S -&gt; SS+ | SS* | a
</code></pre>
<p>step1. 提取左公因子</p>
<pre><code>S -&gt; SSA | a
A -&gt; + | *
</code></pre>
<p>step2. 消除左递归</p>
<pre><code>i = 1 
        S -&gt; aB
        B -&gt; SAB | ε
        A -&gt; + | *
i = 2
    j = 1
        S -&gt; aB
        B -&gt; aBAB | ε
        A -&gt; + | *
</code></pre>
<p>step3. 预测分析表</p>
<table>
    <thead>
        <tr>
            <th rowspan="2">非终结符号</th>
            <th colspan="4">输入符号</th>
        </tr>
        <tr>
            <th>+</th>
            <th>*</th>
            <th>a</th>
            <th>$</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <th>S</th>
            <td></td>
            <td></td>
            <td>S -> aB</td>
            <td></td>
        </tr>
        <tr>
            <th>A</th>
            <td>A -> +</td>
            <td>A -> *</td>
            <td></td>
            <td></td>
        </tr>
        <tr>
            <th>B</th>
            <td>B -> ε</td>
            <td>B -> ε</td>
            <td>B -> SAB</td>
            <td>B -> ε</td>
        </tr>
    </tbody>
</table>    
​    
<h3 id="443"><a class="header" href="#443">4.4.3</a></h3>
<p>计算练习 4.2.1 的文法的 FIRST 和 FOLLOW 集合。</p>
<h4 id="解答-2"><a class="header" href="#解答-2">解答</a></h4>
<ul>
<li>first(S) = [a]</li>
<li>follow(S) = [a, +, *]</li>
</ul>
<h3 id="444"><a class="header" href="#444">4.4.4</a></h3>
<p>计算练习 4.2.2 中各个文法的 FIRST 和 FOLLOW 集合。</p>
<h4 id="解答-3"><a class="header" href="#解答-3">解答</a></h4>
<ol>
<li>
<p>S -&gt; 0 S 1 | 0 1</p>
<ul>
<li>first(S) = [0]</li>
<li>follow(S) = [1, $]</li>
</ul>
</li>
<li>
<p>S -&gt; + S S | * S S | a</p>
<ul>
<li>first(S) = [+, *, a]</li>
<li>follow(S) = [+, *, a, $]</li>
</ul>
</li>
<li>
<p>S -&gt; S (S) S | ε</p>
<ul>
<li>first(S) = [(, ε]</li>
<li>followS(S) = [), $]</li>
</ul>
</li>
<li>
<p>S -&gt; S + S | S S | (S) | S * | a</p>
<ul>
<li>first(S) = [(, a]</li>
<li>follow(S) = [+, (, ), a, *, $]</li>
</ul>
</li>
<li>
<p>S -&gt; (L) | a 以及 L -&gt; L, S | S</p>
<ul>
<li>first(S) = [(, a]</li>
<li>follow(S) = [&quot;,&quot;, $]</li>
<li>first(L) = first(S) = [(, a]</li>
<li>follow(L) = [), &quot;,&quot;, $]</li>
</ul>
</li>
<li>
<p>S -&gt; a S b S | b S a S | ε</p>
<ul>
<li>first(S) = [a, b, ε]</li>
<li>follow(S) = [a, b, $]</li>
</ul>
</li>
<li>
<p>下面的布尔表达式对应的文法：</p>
<pre><code> bexpr -&gt; bexpr or bterm | bterm
 bterm -&gt; bterm and bfactor | bfactor
 bfactor -&gt; not bfactor | (bexpr) | true | false
</code></pre>
</li>
</ol>
<h3 id="445"><a class="header" href="#445">4.4.5</a></h3>
<p>文法 S -&gt; aSa | aa 生成了所有由 a 组成的长度为偶数的串。我们可以为这个文法设计一个带回溯的递归下降分析器。如果我们选择先用产生式 S -&gt; aa 展开，那么我们只能识别串 aa。因此，任何合理的递归下降分析器将首先尝试 S -&gt; aSa。</p>
<ol>
<li>！ 说明这个递归下降分析器识别输入 aa，aaaa 和 aaaaaaaa，但识别不了 aaaaaa。</li>
<li>！！ 这个递归下降分析器识别什么样的语言？</li>
</ol>
<hr />
<h3 id="注意"><a class="header" href="#注意">注意</a></h3>
<p>以下题目请参考 Aho 本人的讲义：<a href="http://www.cs.columbia.edu/%7Eaho/cs3261/lectures/12-10-08.htm">Aho: Properties of Context-Free Languages</a>，<a href="cs.columbia.edu-aho-cs3261-properties-of-cfl-121008.html">本地副本</a></p>
<p>此外还有<a href="http://courses.engr.illinois.edu/cs373/Lectures/lec14.pdf">另一篇内容相似的文章</a>，<a href="courses.engr.illinois.edu-cs373-lec14.pdf">本地副本</a></p>
<p>关于 CNF 和 CYK 算法，有较多相关资料，自行搜索</p>
<h3 id="446-"><a class="header" href="#446-">4.4.6 !</a></h3>
<p>如果一个文法没有产生式体为 ε 的产生式，那么这个文法就是无 ε 产生式的。</p>
<ol>
<li>给出一个算法，他的功能是把任何文法转变成一个无 ε 产生式的生成相同语言的文法（唯一可能的例外是空串——没有哪个无 ε 产生式的文法能生成 ε）。提示：首先找出所有可能为空的非终结符号。非终结符号可能为空是指它（可能通过很长的推导）生成 ε。</li>
<li>将你的算法应用于文法 S -&gt; aSbS | bSaS | ε</li>
</ol>
<h3 id="447-"><a class="header" href="#447-">4.4.7 ！</a></h3>
<p>单产生式是指其产生式体为单个非终结符号的产生式，即形如 A -&gt; B 的产生式。</p>
<ol>
<li>给出一个算法，它可以把任何文法转变成一个生成相同语言（唯一可能的例外是空串）的、无 ε 产生式、无单产生式的文法。提示：首先消除 ε 产生式，然后找出所有满足下列条件的非终结符号对 A 和 B：存在 A =*=&gt; B。</li>
<li>将你的算法应用于 4.1.2 节的算法。</li>
<li>说明作为 （1） 的一个结果，我们可以把一个文法转换成一个没有环的等价文法。</li>
</ol>
<h3 id="448-"><a class="header" href="#448-">4.4.8 ！！</a></h3>
<p>如果一个文法的每个产生式要么形如 A -&gt; BC，要么形如 A -&gt; a，那么这个文法就成为 Chomsky 范式（Chomsky Normal Form， CNF）。说明如何将任意文法转变成一个生成相同语言（唯一可能的例外是空串——没有 CNF 文法可以生成 ε）的 CNF 文法。</p>
<h3 id="449-"><a class="header" href="#449-">4.4.9 ！</a></h3>
<p>对于每个具有上下文无关的语法，其长度为 n 的串可以在 O(n^3) 的时间内完成识别。完成这种识别工作的一个简单方法称为 Cocke-Younger-Kasami（CYK）算法。该算法基于动态规划技术。也就是说，给定一个串 a_1a_2…a_n，我们构造出一个 nxn 的表 T 使得 T_ij 是可以生成子串 a_ia_i+1…aj 的非终结符号的集合。如果基础文法是 CNF 的，那么只要我们按照正确的顺序来填表：先填 j-i 值最小的条目，则表中的每一个条目都可以在 O(n) 时间内填写完毕。给出一个能够正确填写这个表的条目的算法，并说明你的算法的时间复杂度为  O(n^3)。填完这个表之后，你如何判断 a_1a_2…a_n 是否在这个语言中？</p>
<h3 id="4410-"><a class="header" href="#4410-">4.4.10 ！</a></h3>
<p>说明我们如何能够在填好练习 4.4.9 中的表之后，在 O(n) 的时间内获得 a_1a_2…a_n 对应的一颗语法分析树？提示：修改练习 4.4.9 中的表 T，使得对于表的每个条目 T_ij 中的每个非终结符号 A，这个表同时记录了其他条目中的哪两个非终结符号组成的对偶使得我们将 A 放到 T_ij 中。</p>
<h3 id="4411-"><a class="header" href="#4411-">4.4.11 ！</a></h3>
<p>修改练习 4.4.9 中的算法，使得对于任意符号串，他可以找出至少需要执行多少次插入、删除和修改错误（每个错误是一个字符）的操作才能将这个串变成基础文法的语言的句子。</p>
<h3 id="4412-"><a class="header" href="#4412-">4.4.12 ！</a></h3>
<pre><code>stmt -&gt; if e then stmt stmtTail
      | while e do stmt
      | begin list end
      | s
stmtTail -&gt; else stmt
          | ε
list -&gt; stmt listTail
listTail -&gt; ; list
          | ε
</code></pre>
<p>上面的代码给出了对应于某些语句的文法。你可以将 e 和 s 当做分别代表条件表达式和“其他语句”的终结符号。如果我们按照下列方法来解决因为展开可选“else”（非终结符号 stmtTail）而引起的冲突：当我们从输入中看到一个 else 时就选择消耗掉这个 else。使用 4.4.5 节中描述的同步符号的思想：</p>
<ol>
<li>为这个文法构造一个带有错误纠正信息的预测分析表。</li>
<li>给出你的语法分析器在处理下列输入时的行为：
<ol>
<li>if e then s; if e then s end</li>
<li>while e do begin s; if e then e; end</li>
</ol>
</li>
</ol>

                    </main>

                    <nav class="nav-wrapper" aria-label="Page navigation">
                        <!-- Mobile navigation buttons -->
                            <a rel="prev" href="../../ch04/4.3/4.3.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
                                <i class="fa fa-angle-left"></i>
                            </a>

                            <a rel="next" href="../../ch04/4.5/4.5.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
                                <i class="fa fa-angle-right"></i>
                            </a>

                        <div style="clear: both"></div>
                    </nav>
                </div>
            </div>

            <nav class="nav-wide-wrapper" aria-label="Page navigation">
                    <a rel="prev" href="../../ch04/4.3/4.3.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
                        <i class="fa fa-angle-left"></i>
                    </a>

                    <a rel="next" href="../../ch04/4.5/4.5.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
                        <i class="fa fa-angle-right"></i>
                    </a>
            </nav>

        </div>




        <script type="text/javascript">
            window.playground_copyable = true;
        </script>


        <script src="../../elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
        <script src="../../mark.min.js" type="text/javascript" charset="utf-8"></script>
        <script src="../../searcher.js" type="text/javascript" charset="utf-8"></script>

        <script src="../../clipboard.min.js" type="text/javascript" charset="utf-8"></script>
        <script src="../../highlight.js" type="text/javascript" charset="utf-8"></script>
        <script src="../../book.js" type="text/javascript" charset="utf-8"></script>

        <!-- Custom JS scripts -->


    </body>
</html>
